﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lbCheck.Text = "";
        string number = @"^\d{9,10}$";
        string email = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        if (TextBox1.Text == "")
        {
            TextBox1.Focus();
            lbCheck.Text = "Please enter title!";
        }
        else if (TextBox2.Text == "")
        {
            TextBox2.Focus();
            lbCheck.Text = "Please enter your email!";
        }
        else if (TextBox3.Text == "")
        {
            TextBox3.Focus();
            lbCheck.Text = "Please enter your phone number!";
        }
        else if (TextBox4.Text == "")
        {
            TextBox4.Focus();
            lbCheck.Text = "Please enter subject!";
        }
        else if (!Regex.IsMatch(TextBox2.Text, email))
        {
            lbCheck.Text = "Email is invalid!";
        }
        else if (!Regex.IsMatch(TextBox3.Text, number))
        {
            lbCheck.Text = "Number have to be from 9 to 10 characters!";
        }
        else
            try
            {

                MailMessage mail = new MailMessage();

                mail.To.Add(new MailAddress("dungvdgc00509@fpt.edu.vn"));
                mail.Subject = TextBox1.Text;
                mail.Body = "Email: " + TextBox2.Text + "\n"
                    + "Phone: " + TextBox3.Text + "\n"
                    + "Complain: " + TextBox4.Text;

                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = true;
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                smtp.Send(mail);
                lbCheck.Text = "You have contact us successfully!";

            }

            catch (SmtpException smtp)
            {
            }
    }
}